//
//  SJZLoginQuickViewController.m
//  SJZMetro
//
//  Created by 彭利民 on 2017/5/15.
//  Copyright © 2017年 bwton. All rights reserved.
//

#import "SJZLoginQuickViewController.h"

#import "VIZCodeButton.h"
#import "SJZHomePageViewController.h"
#import "SJZStatisticalViewController.h"
#import "SJZHomePageViewController.h"
#import "SJZMineViewController.h"
#import "YQLHttpHandler.h"

@interface SJZLoginQuickViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *phoneTextField;
@property (strong, nonatomic) IBOutlet UITextField *codeTextField;
@property (strong, nonatomic) IBOutlet UITextField *yaoqingTextField;
@property (strong, nonatomic) IBOutlet VIZCodeButton *getCodeBtn;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerbtn;
@property (weak, nonatomic) IBOutlet UIButton *accountPwdbtn;
@property (strong, nonatomic) IBOutlet UIView *yaoqingView;

@end

@implementation SJZLoginQuickViewController

// 会员协议按钮点击
- (IBAction)huiyuanBtnClick:(id)sender {
    
    NSLog(@"会员协议按钮点击");

    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
#warning TODO

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//- (void)popself{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    self.loginBtn.backgroundColor = kRGBA(245, 77, 83, 1);
    self.loginBtn.layer.cornerRadius = 5;
    self.loginBtn.clipsToBounds = YES;
    self.getCodeBtn.layer.cornerRadius = 5;
    self.getCodeBtn.clipsToBounds = YES;
    
    self.codeTextField.delegate = self;
    self.phoneTextField.delegate = self;
    
    [self.registerbtn setTitleColor:kRGBA(245, 77, 83, 1) forState:UIControlStateNormal];
    [self.accountPwdbtn setTitleColor:kRGBA(245, 77, 83, 1) forState:UIControlStateNormal];
//
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"快速登录";
//    0016
    self.navigationController.navigationBar.backgroundColor = kRGBA(245, 77, 83, 1);
    
}

- (void)setUI{
    
    self.yaoqingView.hidden = YES;
    
    self.getCodeBtn.runBgColor = [UIColor lightGrayColor];
    self.getCodeBtn.runTextColor = [UIColor whiteColor];
    self.getCodeBtn.seconds = 60;
    __weak typeof (self.getCodeBtn) weakcodeBtn = self.getCodeBtn;
    self.getCodeBtn.willEndBlock = ^(){

        [weakcodeBtn setTitle:@"重新获取验证码" forState:0];
        [weakcodeBtn setBackgroundColor:kRGBA(245, 77, 83, 1)];
        weakcodeBtn.enabled = YES;
        
    };
    
}


- (IBAction)getCodeBtnClick:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (self.phoneTextField.text.length != 11) {
        [MBProgressHUD showErrorMessage:@"请输入正确手机号"];
        return;
    }
    [MBProgressHUD showProgressMessageInView:@"请稍后"];
    [YQLHttpHandler sendCodeWithPhone:self.phoneTextField.text completion:^(BOOL isSuccess, NSString *errStr) {
        [MBProgressHUD hideHUD];
        if (isSuccess) {
            [MBProgressHUD showSuccessMessage:@"获取验证码成功"];

        }else{
            [MBProgressHUD showErrorMessage:errStr];
        }
    }];
    [self.getCodeBtn starteCode];

}

- (IBAction)loginBtnClick:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
//#warning VIZTODO 测试
//    SJZAccountModel *account = [[SJZAccountModel alloc] init];
//    account.phone = @"17600105324";
//    account.merchantId = @"X79180425105815";
//    account.token = @"03D50B1CFF404E249A345578FACFD9A4";
//    [SJZAccountModel saveUserObjectWithUser:account];
//
//
//    UITabBarController *rootVC = [TabNavModuleAPI rootTabBarCcontroller];
//    [UIApplication sharedApplication].keyWindow.rootViewController = rootVC;
//
//    return;
    
    NSLog(@"登录点击了");
    
    if (self.phoneTextField.text.length < 11) {
        [MBProgressHUD showErrorMessage:@"请输入正确手机号"];
        return;
    }
    if (self.codeTextField.text.length < 4) {
        [MBProgressHUD showErrorMessage:@"请输入正确验证码"];
        return;
    }
//    13564923163
    [MBProgressHUD showProgressMessageInView:@"登录中"];
    [YQLHttpHandler loginWithPhone:self.phoneTextField.text code:self.codeTextField.text completion:^(BOOL isSuccess, NSString *errStr, SJZAccountModel *account) {
        if (!errStr.length) {
            account.phone = self.phoneTextField.text;
            [SJZAccountModel saveUserObjectWithUser:account];
            [[NSNotificationCenter defaultCenter] postNotificationName:kLoginStatusChange object:nil];
        }else{
            [MBProgressHUD showErrorMessage:errStr];
        }
    }];
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.phoneTextField) {
        
        return [UITextField textField:textField shouldChangeCharactersInRange:range replacementString:string andCanInputString:@"1234567890" andLength:11];
        
    }else if (textField == self.codeTextField){
        
        return [UITextField textField:textField shouldChangeCharactersInRange:range replacementString:string andCanInputString:@"1234567890" andLength:8];

    }
    
    return YES;
    
}




@end
